package com.yjt.class_studio.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjt.class_studio.entity.CmsStudio;
import com.yjt.class_studio.entity.CmsTeam;
import com.yjt.class_studio.entity.SysUser;
import com.yjt.class_studio.pojo.dto.CmsStudioSearchDTO;
import com.yjt.class_studio.pojo.dto.CmsTeamSearchDTO;
import com.yjt.class_studio.service.CmsStudioService;
import com.yjt.class_studio.service.CmsTeamService;
import com.yjt.class_studio.service.SysUserService;
import com.yjt.class_studio.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Yjt
 * @Date 2023/6/19 18:43
 * @Description
 */
@Api(tags = "团队管理")
@RequestMapping("/team")
@RestController
public class CmsTeamController {

    @Autowired
    private CmsStudioService cmsStudioService;

    @Autowired
    private CmsTeamService cmsTeamService;

    @ApiOperation(value = "分页查询团队")
    @PostMapping("/getTeamList")
    public R getTeamList(@RequestBody CmsTeamSearchDTO cmsTeamSearchDTO){
       Page<CmsTeam> pagae = new Page<>(cmsTeamSearchDTO.getPageNum(), cmsTeamSearchDTO.getPageSize());
       IPage<CmsTeam> team = cmsTeamService.page(pagae, new QueryWrapper<CmsTeam>().like("team_name", cmsTeamSearchDTO.getTeamName()));
        team.getRecords().forEach(item->{
            CmsStudio studio = cmsStudioService.getById(item.getStudioId());
            item.setStudioName(studio.getStudioName());
        });
        return R.ok().put("teamList",team);
   }

    @GetMapping("/getTeamById")
    @ApiOperation(value = "根据id查询团队")
    public R getTeamById(@RequestParam Integer id){
       CmsTeam team = cmsTeamService.getById(id);
       return R.ok().put("team",team);
   }

    @PostMapping("/saveTeam")
    @ApiOperation(value = "新增团队")
    public R saveTeam(@RequestBody CmsTeam cmsTeam){ cmsTeam.setCreateTime(new Date());
       boolean b = cmsTeamService.save(cmsTeam);
       if(b)
           return R.ok("新增成功");
       else
           return R.error("新增失败");
   }

    @PostMapping("/updateTeam")
    @ApiOperation(value = "更新团队")
    public R updateTeam(@RequestBody CmsTeam cmsTeam){
       CmsTeam team = cmsTeamService.getById(cmsTeam.getId());
       if(team != null){
           cmsTeam.setUpdateTime(new Date());
           boolean b = cmsTeamService.updateById(cmsTeam);
           if(b)
               return R.ok("更新成功");
           else
               return R.error("更新失败");
       }else{
           return R.error("找不到此条数据");
       }
   }

   @DeleteMapping("/deleteTeam")
    @ApiOperation(value = "删除团队")
    public R deleteTeam(Integer id){
       boolean b = cmsTeamService.removeById(id);
       if(b)
           return R.ok("删除成功");
       else
           return R.error("删除失败");
   }
}
